//
//  MainTabBar.swift
//  微博模拟项目
//
//  Created by 尹家鑫 on 16/3/12.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    //父类有这个方法 子类需要做其他的操作 需要重写父类的方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        addSubview(plusBtn)
    }
    
    //从xib/sb中加载视图控件的时候会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //自定义tabbar
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //UITabBarButton 是私有类
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个递增的索引
        var index: CGFloat = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //修改subView的frame
                subView.frame = CGRectOffset(rect, index * w, 0)
                
                index += (index == 1 ? 2 : 1)
            }
        }
        
        addSubview(plusBtn)
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
    }
    
    //MARK:懒加载子视图
    lazy var plusBtn: UIButton = {
        //就是自定义样式的按钮
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        return btn
    }()
    
}
